package com.zhixin.fans.ks.ui.charge;

import android.arch.lifecycle.Observer;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.zhixin.fans.ks.R;
import com.zhixin.fans.ks.api.CommonApi;
import com.zhixin.fans.ks.api.res.OrderInfo;
import com.zhixin.fans.ks.api.res.UserInfo;
import com.zhixin.fans.ks.api.res.VipOption;
import com.zhixin.fans.ks.api.res.WxPayInfo;
import com.zhixin.fans.ks.manager.UserInfoManager;
import com.zhixin.fans.ks.ui.base.BaseNeedLoginActivity;
import com.zhixin.fans.ks.ui.utils.PayUtil;

import org.eenie.common.subscribes.ProgressSubscriber;
import org.eenie.common.views.QMUIWindowInsetLinearLayout;
import org.eenie.common.views.TopBarLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;


public class ChargeVipActivity extends BaseNeedLoginActivity implements Observer<UserInfo>, BaseQuickAdapter.OnItemClickListener {
    public static final String ROUTE_PATH = "/charge/charge_vip";
    @BindView(R.id.top_bar)
    TopBarLayout mTopBar;
    @BindView(R.id.img_user_header)
    ImageView mImgUserHeader;
    @BindView(R.id.rv_vip_option)
    RecyclerView mRvVipOption;
    @BindView(R.id.fragment_container)
    QMUIWindowInsetLinearLayout mFragmentContainer;
    @BindView(R.id.tv_user_name)
    TextView mTvUserName;
    @BindView(R.id.tv_user_id)
    TextView mTvUserId;

    ChargeVipAdapter mChargeVipAdapter;
    List<VipOption> mVipOptions = new ArrayList<>();


    CommonApi mCommonApi;

    UserInfoManager mUserInfoManager;
    @BindView(R.id.btn_vip_state)
    QMUIRoundButton mBtnVipState;

    @Override
    public int initContentView() {
        return R.layout.activity_charge_vip;
    }

    @Override
    public void initInjector() {
        mCommonApi = getApplicationComponent().getApiManager().create(CommonApi.class);
        mUserInfoManager = getCoupleAppComponent().getUserInfoManager();

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mRvVipOption.setLayoutManager(new LinearLayoutManager(mContext));
        mRvVipOption.setAdapter(mChargeVipAdapter = new ChargeVipAdapter(mVipOptions));
        mChargeVipAdapter.setOnItemClickListener(this);

        mUserInfoManager.observe(this, this);
        mUserInfoManager.update();
        fetchVipList();
    }


    private void fetchVipList() {
        mCommonApi.fetchVipList()
                .subscribe(new ProgressSubscriber<List<VipOption>>() {
                    @Override
                    public void onNext(List<VipOption> vipOptions) {
                        mVipOptions.clear();
                        mVipOptions.addAll(vipOptions);
                        mChargeVipAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }


    @Override
    public void onChanged(@Nullable UserInfo userInfo) {
        if (userInfo != null && userInfo.getU_id() > 0) {
            Glide.with(mContext).asBitmap().load(userInfo.getU_headPicture()).into(mImgUserHeader);
            mTvUserName.setText(userInfo.getU_nickName());
            mTvUserId.setText(String.format("ID：%s", userInfo.getU_id()));
            mBtnVipState.setText(userInfo.getU_isVip() == 0 ? "你还不是VIP" : "VIP");
        }

    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        VipOption option = (VipOption) adapter.getItem(position);
        submitPayOrder(option.getV_id());
    }


    private void submitPayOrder(int goodId) {
        mCommonApi.submitPayOrder("VIP", goodId)
                .flatMap(new Function<OrderInfo, ObservableSource<WxPayInfo>>() {
                    @Override
                    public ObservableSource<WxPayInfo> apply(OrderInfo orderInfo) throws Exception {
                        return mCommonApi.fetchPayInfo(orderInfo.getOrderId());
                    }
                })
                .subscribe(new ProgressSubscriber<WxPayInfo>(mContext) {
                    @Override
                    public void onNext(WxPayInfo payInfo) {
                        PayUtil.payWx(mContext, payInfo);
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }
}
